package com.yeshine.opendata.service.impl;

import com.yeshine.opendata.core.ControllerBase;
import com.yeshine.opendata.core.ServiceBase;
import com.yeshine.opendata.dao.IOrgDao;
import com.yeshine.opendata.entity.Org;
import com.yeshine.opendata.entity.User;
import com.yeshine.opendata.service.IOrgService;
import com.yeshine.opendata.utils.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;


/**
 * Created by taohj on 2014/6/28.
 */
@Service
public class OrgServiceImpl extends ServiceBase<Org> implements IOrgService {


    private ChineseCharToEn charToEn=new ChineseCharToEn();

    private final String path= UploadConstantPath.ORG_HEADIMAGE_PATH;


    private IOrgDao dao;

    @Autowired
    public void setDao(IOrgDao dao) {
        this.dao = dao;
    }

    @Override
    public Result<Org> create(Org org) {
        org.setFirstLetter(charToEn.getAFirstLetter(org.getName()));
        org.setCreater((User) ControllerBase.getSession().getAttribute("user"));
        org.setCreateTime(new Date());
        if (dao.insert(org) > 0){
            return new Result<Org>("新增成功");
        }else{
            return new Result<Org>(false,"新增失败");
        }
    }

    @Override
    public Result<Org> remove(Org org) {
        if (dao.delete(org) > 0){
            return new Result<Org>("删除成功");
        }else{
            return new Result<Org>(false,"删除失败");
        }
    }

    @Override
    public Result<Org> modify(Org org) {
        org.setFirstLetter(charToEn.getAFirstLetter(org.getName()));
        if (dao.update(org)>0){
            return new Result<Org>("更新成功");
        }else{
            return new Result<Org>(false,"更新失败");
        }

    }

    @Override
    public Result<Org> getById(Integer id) {
        return new Result<Org>(true, dao.selectById(id));
    }

    @Override
    public Result<Org> getList(Org org) {
        return new Result<Org>(true, dao.selectList(org));
    }

    @Override
    public Result<Org> getByPage(PageResult<Org> pageResult) {
        pageResult.setRows(dao.selectbyPage(pageResult));
        return new Result<Org>(true, pageResult);
    }

    @Override
    public Result<Org> create(Org org, HttpServletRequest request) {
        String path;
        String fileName="";
        Result<Org> result;
        MultipartFile file=org.getHeadImageFile();
        result=this.create(org,request);
        if (!result.isSuccess()){
            return result;
        }
        if (file!=null && file.getSize()>0){


            path = request.getSession().getServletContext().getRealPath(this.path);
            fileName = org.getId()+"_"+file.getOriginalFilename();
            File target = new File(path, fileName);
            try {
                FileUtils.copy(file.getInputStream(), target);
            } catch (IOException e) {
                e.printStackTrace();
                return new Result<Org>(false,"复制文件失败");
            }
            org.setHeadimage(this.path+fileName);
        }
        result=this.modify(org);
        return result;
    }




    @Override
    public Result<Org> modify(Org org, HttpServletRequest request) {

        Result<Org> result;
        String path="";
        String fileName="";
        MultipartFile file=org.getHeadImageFile();
        if (file!=null && file.getSize()>0){
            path = request.getSession().getServletContext().getRealPath(this.path);
            fileName = org.getId()+"_"+file.getOriginalFilename();
            File targetFile = new File(path, fileName);
            try {
                FileUtils.copy(file.getInputStream(), targetFile);
                org.setHeadimage(this.path+fileName);
            } catch (IOException e) {
                result=new Result<Org>(false,e.getMessage());
                e.printStackTrace();
                return result;
            }
        }
        return this.modify(org);
    }

    @Override
    public Result<Org> audit(Integer [] orgs, Integer auditStatus) {
        return new Result<Org>(dao.audit(orgs,auditStatus)>0);
    }

    @Override
    public Result<Org> addMember(Integer org,Integer [] users, @Param("role") Integer role) {
        return new Result<Org>(dao.addMember(org,users,role)>0);
    }

    @Override
    public Result<Org> modifyMember(@Param("org") Integer org, @Param("users") Integer [] users, @Param("role") Integer role) {
        return new Result<Org>(dao.updateMember(org,users,role)>0);
    }

    @Override
    public Result<Org> removeMember(@Param("org") Integer org, @Param("users") Integer [] users) {
        return new Result<Org>(dao.deleteMember(org,users)>0);
    }

}
